/******************************************************************
 * Created by: Goh Chee Hong                                       
 * Date of Creation: 1/Jan/2009     
 * 
 *                                                                
 * Description: The form that handles all file transfers
 ******************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Pbdms.BusinessLogic.FileTransfer;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Tools
{
    public partial class FrmFileTransfers : newForms
    {
        public delegate void DelegateWorker(object sender, string args);
        public FrmFileTransfers()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(FrmFileTransfers_FormClosing);
            Color c= new Color();
            c=panelDisplay.BackColor;
            Pbdms.Common.Global.paintBackGround(this);
            
            panelDisplay.BackColor=c;
        }

        void FrmFileTransfers_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;

            this.Hide();
            
        }
        /// <summary>
        /// add a file transfer object
        /// </summary>
        /// <param name="fileTransfer"></param>
        public void addControl(FileTransferGui fileTransfer)
        {
            
            fileTransfer.CancelClicked += new EventHandler(file_CancelClicked);
            fileTransfer.Dock = DockStyle.Top;
            fileTransfer.start();
            this.panelDisplay.Controls.Add(fileTransfer);
             
        }

        public override void changeName(string keyID, string oldName, string newName)
        {
            Control.ControlCollection controls = panelDisplay.Controls;
            int count = controls.Count;
      
            for(int i=0; i<count;i++)
            {
                FileTransferGui guiControl=(FileTransferGui)controls[i];
                guiControl.changeName(keyID,  oldName,  newName);
            }
        }
        /// <summary>
        /// remove the user control object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void file_CancelClicked(object sender, EventArgs e)
        {
            FileTransferGui file = (FileTransferGui)sender;
            this.panelDisplay.Controls.Remove(file);
            //file.releaseResources();
        }

       
        /// <summary>
        /// clear all finished file transfer object
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            Control.ControlCollection listofcontrol=panelDisplay.Controls;
            int i = 0;
            for(i=0; i<listofcontrol.Count;i++)
            {
                FileTransferGui control = (FileTransferGui)listofcontrol[i];
                
                if (control.Finished() == true)
                {
                    control.btnCancel_Click(control, EventArgs.Empty);
                    i--;
                }
                
            }
        }
        /// <summary>
        /// cancel all file transfers
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Control.ControlCollection listofcontrol = panelDisplay.Controls;
            int i = 0;
            for (i = 0; i < listofcontrol.Count; i++)
            {
                FileTransferGui control = (FileTransferGui)listofcontrol[i];
                {
                    control.btnCancel_Click(control, EventArgs.Empty);
                    i--;
                }
            }
        }
    }
}